#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/core/core.hpp>
#include <QFileDialog>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_btnLoadPic_clicked()
{
  QString imagePath=  QFileDialog::getOpenFileName(this,tr("打开图像"),"d:/words/ddd","(allPic(*.jpg *.png *.bmp))");

  if(imagePath.isEmpty())
  {
      return;
  }
    srcImage=imread(imagePath.toStdString());
    cvtColor(srcImage,srcImage,CV_BGR2RGB);
    QImage displayImage=QImage(srcImage.data,srcImage.cols,srcImage.rows,srcImage.cols*srcImage.channels(),QImage::Format_RGB888);
    QImage disImage=imageCenter(displayImage,ui->label_show1);
    ui->label_show1->setPixmap(QPixmap::fromImage(disImage));

}

QImage MainWindow::imageCenter(QImage qimage, QLabel *qlabel)
{
    QImage image;
    QSize imageSize=qimage.size();
    QSize labelsize=qlabel->size();
    double dWidthRatio=1.0*imageSize.width()/labelsize.width();
     double dHeightRatio=1.0*imageSize.height()/labelsize.height();
     if(dWidthRatio>dHeightRatio)
     {
         image=qimage.scaledToWidth(labelsize.width());
     }else
     {
         image=qimage.scaledToHeight(labelsize.height());
     }
     return  image;

}


/*图变成黑白*/
void MainWindow::on_btnGray_clicked()
{
    Mat resImage;
    cvtColor(srcImage,resImage,COLOR_BGR2BGRA);
    cvtColor(resImage,resImage,COLOR_GRAY2BGR);
    QImage displayImage=QImage(resImage.data,resImage.cols,resImage.rows,resImage.cols*resImage.channels(),QImage::Format_RGB888);
    QImage disImage=imageCenter(displayImage,ui->label_show2);
    ui->label_show2->setPixmap(QPixmap::fromImage(disImage));
    qDebug("aaaaaaaaa");
}



/*图变的模糊,抑制噪点*/
void MainWindow::on_btnAvager_clicked()
{
    Mat blurImage;
    blur(srcImage,blurImage,Size(4,4));
    QImage displayImage=QImage(blurImage.data,blurImage.cols,blurImage.rows,blurImage.cols*blurImage.channels(),QImage::Format_RGB888);
    QImage disImage=imageCenter(displayImage,ui->label_show2);
    ui->label_show2->setPixmap(QPixmap::fromImage(disImage));

}
/*边缘检测*/
void MainWindow::on_btnEadge_clicked()
{

    if(srcImage.empty())
    {
        return;
    }
    Mat EdgeImage,grayImage;
    cvtColor(srcImage,grayImage,COLOR_BGR2BGRA);
    Canny(grayImage,EdgeImage,200,1);
    cvtColor(EdgeImage,EdgeImage,COLOR_GRAY2BGR);

    QImage displayImage=QImage(EdgeImage.data,EdgeImage.cols,EdgeImage.rows,EdgeImage.cols*EdgeImage.channels(),QImage::Format_RGB888);
    QImage disImage=imageCenter(displayImage,ui->label_show2);
    ui->label_show2->setPixmap(QPixmap::fromImage(disImage));
}
